//
//  WAWeatherView.m
//  WeatherApp
//
//  Created by Yuliya Grasevych on 29.07.14.
//  Copyright (c) 2014 Yuliya Grasevych. All rights reserved.
//

#import "WAWeatherView.h"
#import "WAWeatherProvider.h"
#import "WACurrentWeather.h"
#import "WACustomImageView.h"
#import "NSObject+CastToClass.h"
#import "NSView+FillWithView.h"
#import <Quartz/Quartz.h>

@interface WAWeatherView ()

@property (weak) IBOutlet WACustomImageView *weatherBackground;
- (IBAction)removeCity:(id)sender;

@end

@implementation WAWeatherView
-(id)init
{
    return [super init];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
-(void)awakeFromNib
{
    [super awakeFromNib];
}
-(void)loadView
{
    [super loadView];
    [self observeLocationForKey:self.representedObject];
    
    [self setWeatherInfo];
    
    WACurrentWeather *weather = [[[WAWeatherProvider sharedInstance] locationsWeathers] objectForKey:self.locationKey];
    // set background image after transition to let view load constraints
    [self performSelector:@selector(transitionToImage:) withObject:weather.weatherBg afterDelay:0.1];
}

-(void)observeLocationForKey:(NSObject *)locationKey
{
    _locationKey = locationKey;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(weatherDidUpdate:) name:kLocationWeatherDidUpdate object:locationKey];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(weatherBackgroundDidUpdate:) name:kLocationWeatherBackgroundDidUpdate object:locationKey];
}

-(void)setWeatherInfo
{
    [self willChangeValueForKey:@"representedObject"];
    [self didChangeValueForKey:@"representedObject"];
//    WACurrentWeather *weather = [[[WAWeatherProvider sharedInstance] locationsWeathers] objectForKey:self.locationKey];
//    [self.weatherObjectController setContent:weather];
//    
//    WALocation *location = [[WAWeatherProvider sharedInstance] locationForKey:self.locationKey];
//    [self.locationObjectController setContent:location];
}

-(void)weatherDidUpdate:(NSNotification *)notification
{
    if (![self.locationKey isEqual:notification.object]) {
        
        return;
    }
    [self setWeatherInfo];
}

-(void)weatherBackgroundDidUpdate:(NSNotification *)notification
{
    
    if (![self.locationKey isEqual:notification.object]) {
        
        return;
    }
    WACurrentWeather *weather = [[[WAWeatherProvider sharedInstance] locationsWeathers] objectForKey:notification.object];
    [self transitionToImage:weather.weatherBg];
}

-(void)transitionToImage:(NSImage *)newImage
{
    if (newImage) {
        
        [self.weatherBackground setImage:newImage];
    }
    else {
        
        [self.weatherBackground setImage:[NSImage imageNamed:@"no_image"]];
    }
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



- (IBAction)removeCity:(id)sender
{
    [[WAWeatherProvider sharedInstance] removeLocation:self.locationKey];
}
@end
